package com.yxw.system.exception;

import com.yxw.common.result.Result;
import com.yxw.common.result.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;



/**
 * @className: com.yxw.system.exception.GlobalExceptionHandler
 * @description: 全局异常处理类
 * @author: tommy
 * @create: 2023-07-22 下午 06:39
 */

@ControllerAdvice
public class GlobalExceptionHandler {
    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody //还回一个json数据
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail().message("全局异常");
    }
     //特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e) {
        e.printStackTrace();
        return Result.fail().message("特定异常");
    }
    //自定义异常处理
    @ExceptionHandler(YXWException.class)
    @ResponseBody
    public Result error(YXWException e) {
        e.printStackTrace();
        return Result.fail().code(e.getCode()).message(e.getMessage());
    }

    /**
     * spring security异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.fail().code(ResultCodeEnum.PERMISSION.getCode()).message("没有当前功能的操作权限");
    }

}
